package com.landiao.ctrler;

import java.io.File;
import java.io.FileOutputStream;
import java.util.Date;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.commons.CommonsMultipartFile;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.landiao.common.Constant;
import com.landiao.common.factory.ResultMsgFactory;
import com.landiao.common.pojo.ClientUserInfo;
import com.landiao.common.pojo.ResultMsg;
import com.landiao.common.utils.CodecUtils;
import com.landiao.common.utils.CommUtil;
import com.landiao.common.utils.IpUtil;
import com.landiao.entities.BackstageUser;
import com.landiao.entities.Shop;
import com.landiao.service.BackstageUserService;
import com.landiao.service.ShopService;

@Controller
@RequestMapping("/management")
public class ManagementGlobalCtrler {

	@Autowired
	private BackstageUserService backUserService;
	
	@Autowired
	private ShopService shopService;

	@RequestMapping("/index")
	public String index() {
		return "management/index";
	}

	@RequestMapping(value = "/login", method = RequestMethod.GET)
	public String login(HttpSession session){
		return "management/login";
	}

	@RequestMapping(value = "/login", method = RequestMethod.POST)
	public String login(HttpServletRequest req,BackstageUser userVo, HttpSession session,
			RedirectAttributes redirectAttr) {
		if (!StringUtils.isEmpty(userVo.getAccount())
				&& !StringUtils.isEmpty(userVo.getPasswd())) {
			BackstageUser userPO = backUserService.getByAccount(userVo.getAccount());
			if (userPO!=null&&userPO.getPasswd().equals(CodecUtils.password(userVo.getPasswd()))) {
				Shop shop = shopService.getById(userPO.getShopId());
				ClientUserInfo client = new ClientUserInfo();
		        client.setIp(IpUtil.getIpAddr(req));
		        client.setLogindatetime(new Date());
		        client.setUser(userPO);
		        if(shop!=null&&shop.getPshopId()!=null){
		        	client.setUserType(Constant.USER_TYPE_BRACH);
		        }else{
		        	client.setUserType(Constant.USER_TYPE_MAIN);
		        }
				session.setAttribute(Constant.SESSION_USER, client);
				return "redirect:/management/index";
			} else {
				redirectAttr.addFlashAttribute("resultMsg",
						ResultMsgFactory.fail("用户名或密码错误，请重新输入！"));
				redirectAttr.addFlashAttribute("user", userVo);
				return "redirect:/management/login";
			}
		} else {
			redirectAttr.addFlashAttribute("resultMsg",
					ResultMsgFactory.fail("请填写用户名和密码！！！！！"));
			redirectAttr.addFlashAttribute("user", userVo);
			return "redirect:/management/login";
		}
	}

	@RequestMapping("/logout")
	public String logout(HttpSession session) {
		session.removeAttribute(Constant.SESSION_USER);
		return "management/login";
	}
	
	@ResponseBody
	@RequestMapping(value = "/file",method = RequestMethod.POST)
	public ResultMsg file(@RequestParam("imageFile")CommonsMultipartFile photo,HttpServletRequest request) {
		ResultMsg resultMsg = null;
		if(!photo.isEmpty()){
			String filename = CodecUtils.getUUID()+"."+photo.getContentType().split("/")[1];
			String path = request.getSession().getServletContext().getRealPath("/")+"temp";
			System.out.println(path);
			File newImage = new File(path,filename);
			//String url = "http://localhost:8080/landiao/temp/" + filename;
			String url=request.getScheme()+ "://"+request.getServerName()+ ":" +
					request.getServerPort()+ request.getContextPath() + "/temp/"+filename;
			try{
				//文件复制封装成方法
				CommUtil.copy(photo.getInputStream(), new FileOutputStream(newImage));

			} catch (Exception exception) {
				exception.printStackTrace();
			} 
			resultMsg = ResultMsgFactory.success(url);
		}else{
			resultMsg = ResultMsgFactory.failMsg();
		}
		return resultMsg;
	}
}
